let mana;
let updateData;
let openId;
let pageState;//0,1,2分别为个人界面状态，历史记录状态，审批状态
let manaList;
let uncheckManaList;
let manaMainPage;
let historyMoudle;
let historyHeader;
let Footer;
let historyPage;
let checkHeader;
let checkPage;
let checkMoudle;
let user;
let checkUser;

function back() {
    $("#back").addClass("block-disable");
    $("#fatherBox").removeClass("block-updown-padding block-padding")
    if (pageState === 1) {
        pageState = 0;
        document.getElementById('fatherBox').innerHTML = manaMainPage;
        open();
    } else if (pageState === 2) {
        pageState = 0;
        document.getElementById('fatherBox').innerHTML = manaMainPage;
        open();
    }
}

//2为同意
function agree(obj) {
    let bId = $(obj).attr('name');
    $.ajax({
        method:'GET',
        async:false,
        url:'/management/getBookingByID?id=' + bId,
        success:function(data){
            checkUser = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
    updateData = {
        id: bId,
        meeting_room_id: document.getElementById('opion_' + mana.js_bookingID).getAttribute('name'),
        user_id: checkUser['user_id'],
        purpose: document.getElementById('checkPur_' + mana.js_bookingID).innerHTML,
        participants: parseInt(document.getElementById('checkPar_' + mana.js_bookingID).innerHTML),
        time_occupied: document.getElementById('checkTime_' + mana.js_bookingID).innerHTML,
        contact: checkUser['contact'],
        status: 2,
        comment: document.getElementById('checkCom_' + mana.js_bookingID).innerHTML,
        reply: document.getElementById('checkRep_' + mana.js_bookingID).innerHTML,
        real_name: checkUser['real_name']
    };
    $.ajax({
        method: 'GET',
        data: updateData,
        async: false,
        url: '/management/updateBookingState?openid=' + openId,
        success: function (data) {
            console.log(data + 'send successfully');
            back();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
}

//0为不同意
function disagree(obj) {
    let bId = $(obj).attr('name');
    $.ajax({
        method:'GET',
        async:false,
        url:'/management/getBookingByID?id=' + bId,
        success:function(data){
            checkUser = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
    updateData = {
        id: bId,
        meeting_room_id: document.getElementById('opion_' + mana.js_bookingID).getAttribute('name'),
        user_id: checkUser['user_id'],
        purpose: document.getElementById('checkPur_' + mana.js_bookingID).innerHTML,
        participants: parseInt(document.getElementById('checkPar_' + mana.js_bookingID).innerHTML),
        time_occupied: document.getElementById('checkTime_' + mana.js_bookingID).innerHTML,
        contact: checkUser['contact'],
        status: 0,
        comment: document.getElementById('checkCom_' + mana.js_bookingID).innerHTML,
        reply: document.getElementById('checkRep_' + mana.js_bookingID).innerHTML,
        real_name: checkUser['real_name']
    };
    $.ajax({
        method: 'GET',
        data: updateData,
        async: false,
        url: '/management/updateBookingState?openid=' + openId,
        success: function (data) {
            console.log(data + 'send successfully');
            back();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
}

historyHeader = '<div style="position: fixed;background-color: white;width: 100%">\n' +
    '        <ul class="nav nav-pills nav-fill">\n' +
    '            <li class="nav-width">\n' +
    '                <button type="button" class="btn-back" onclick="back()">返回</button>\n' +
    '            </li>\n' +
    '            <li class="nav-width" style="text-align: center">\n' +
    '                <div style="font-size: 20px;font-weight: bold;text-align: center;padding-bottom: 10px;padding-top: 10px">\n' +
    '                    历史记录\n' +
    '                </div>\n' +
    '            </li>\n' +
    '        </ul>\n' +
    '    </div>\n' +
    '    <div class="out-top">\n' +
    '    </div>';

historyPage = historyHeader;

checkHeader = '<!-- 以下为标题等信息 -->\n' +
    '    <div style="position: fixed;background-color: white;width: 100%">\n' +
    '        <ul class="nav nav-pills nav-fill">\n' +
    '            <li class="nav-width">\n' +
    '                <button type="button" class="btn-back" onclick="back()">返回</button>\n' +
    '            </li>\n' +
    '            <li class="nav-width" style="text-align: center">\n' +
    '                <div style="font-size: 20px;font-weight: bold;text-align: center;padding-bottom: 10px;padding-top: 10px">\n' +
    '                    会议室预约审核\n' +
    '                </div>\n' +
    '            </li>\n' +
    '        </ul>\n' +
    '    </div>\n' +
    '    <!-- 以上为标题等信息 -->\n' +
    '    <div class="out-top">\n' +
    '    </div>';

checkPage = checkHeader;

function stateToOpinion(bookingState) {
    let result = "";
    switch (bookingState) {
        case 0:
            result = "未通过";
            break;
        case 1:
            result = "审批中";
            break;
        case 2:
            result = "已通过";
            break;
        default:
            result = "请和管理员联系";
            break;
    }
    return result;
}

function setEle() {

    if (manaList.length === undefined || manaList.length === 0){
        document.getElementById('historyMoudle').innerHTML = '<div>无历史记录</div>';
    }else{
        //document.getElementById('nickName').innerHTML = mana.js_userNickname;
        document.getElementById('meetingDate').innerHTML = mana.js_date;
        document.getElementById('meetingRoomName').innerHTML = mana.js_meetingRoomName;
        document.getElementById('opinion').innerHTML = stateToOpinion(mana.js_bookingState);
        document.getElementById('date').innerHTML = mana.js_date;
        document.getElementById('time').innerHTML = mana.js_beginTime + '-' + mana.js_endTime;
        document.getElementById('people').innerHTML = mana.js_bookingParticipants;
        document.getElementById('purpose').innerHTML = mana.js_bookingPurpose;
        document.getElementById('reply').innerHTML = mana.js_bookingReply;

        $(document).ready(function () {
            switch (mana.js_bookingState) {
                case 0:
                    $("#historyMoudle").addClass("text-backcolorRed");
                    break;
                case 1:
                    $("#historyMoudle").addClass("text-backcolorYellow");
                    break;
                case 2:
                    $("#historyMoudle").addClass("text-backcolorGreen");
                    break;
            }
        });
    }
}

function changeToHistory() {
    $("#back").removeClass("block-disable");
    document.getElementById("header").innerText = "历史记录";
    document.getElementById('fatherBox').innerHTML = "";

    pageState = 1;
    if (manaList.length === undefined || manaList.length === 0) {
        historyPage = historyPage + '<div>无历史记录</div>';
    } else {
        for (let i = 0; i < manaList.length; i++) {
            mana = {
                js_userID: manaList[i]['userID'],
                js_userNickname: manaList[i]['userNickname'],
                js_userRealName: manaList[i]['userRealName'],
                js_meetingRoomName: manaList[i]['meetingRoomName'],
                js_bookingPurpose: manaList[i]['bookingPurpose'],
                js_bookingParticipants: manaList[i]['bookingParticipants'],
                js_date: manaList[i]['bookingTimeOccupied']['date'],
                js_beginTime: manaList[i]['bookingTimeOccupied']['beginTime'],
                js_endTime: manaList[i]['bookingTimeOccupied']['endTime'],
                js_bookingState: manaList[i]['bookingState'],
                js_bookingComment: manaList[i]['bookingComment'],
                js_bookingReply: manaList[i]['bookingReply'],
                js_state: manaList[i]['state'],
                js_bookingID: manaList[i]['bookingID'],
                js_meetingRoomID: manaList[i]['meetingRoomID']
            };
            historyMoudle = '<div class="detail-margin" id="' + mana.js_bookingID + '">\n' +
                '        <div class="container ">\n' +
                '            <div class="row">\n' +
                '                <div class="textFont text-title">\n' + mana.js_date +
                '                    \n' +
                '                </div>\n' +
                '                <div class="textFont text-title1">\n' + mana.js_meetingRoomName +
                '                    \n' +
                '                </div>\n' +
                '                <div class="textFont text-title">\n' + stateToOpinion(mana.js_bookingState) +
                '                    \n' +
                '                </div>\n' +
                '                <div>\n' +
                '                    <hr class="hr-sty">\n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    参会日期:\n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    参会时间:\n' +
                '                </div>\n' +
                '                <div class="text1">\n' + mana.js_date +
                '                    \n' +
                '                </div>\n' +
                '                <div class="text1">\n' + mana.js_beginTime + '-' + mana.js_endTime +
                '                    \n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    参会人数:\n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    会议目的:\n' +
                '                </div>\n' +
                '                <div class="text1">\n' + mana.js_bookingParticipants +
                '                    人\n' +
                '                </div>\n' +
                '                <div class="text1">\n' + mana.js_bookingPurpose +
                '                    \n' +
                '                </div>\n' +
                '                <div class="text1 text1-bottom text-bold">\n' +
                '                    管理员审批意见:\n' +
                '                </div>\n' +
                '                <div class="div-text">\n' +
                '                    <textarea rows="5" cols="20" readonly="readonly" class="textarea1">' + mana.js_bookingReply + '</textarea>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>'
            document.getElementById('fatherBox').innerHTML += historyMoudle;
        }
    }
    $(document).ready(function () {
        $("#fatherBox").addClass("block-padding");
        let t_mana;
        for (let i = 0; i < manaList.length; i++) {
            t_mana = {
                js_bookingID: manaList[i]['bookingID'],
                js_bookingState: manaList[i]['bookingState']
            };
            switch (t_mana.js_bookingState) {
                case 0:
                    $("#" + t_mana.js_bookingID).addClass("text-backcolorRed");
                    break;
                case 1:
                    $("#" + t_mana.js_bookingID).addClass("text-backcolorYellow");
                    break;
                case 2:
                    $("#" + t_mana.js_bookingID).addClass("text-backcolorGreen");
                    break;
            }
        }

    })
    //historyPage = historyPage + Footer;
    //document.getElementById('fatherBox').innerHTML = historyPage;
}

function showRecentMana() {
    mana = {
        js_userID: manaList[0]['userID'],
        js_userNickname: manaList[0]['userNickname'],
        js_userRealName: manaList[0]['userRealName'],
        js_meetingRoomName: manaList[0]['meetingRoomName'],
        js_bookingPurpose: manaList[0]['bookingPurpose'],
        js_bookingParticipants: manaList[0]['bookingParticipants'],
        js_date: manaList[0]['bookingTimeOccupied']['date'],
        js_beginTime: manaList[0]['bookingTimeOccupied']['beginTime'],
        js_endTime: manaList[0]['bookingTimeOccupied']['endTime'],
        js_bookingState: manaList[0]['bookingState'],
        js_bookingComment: manaList[0]['bookingComment'],
        js_bookingReply: manaList[0]['bookingReply'],
        js_state: manaList[0]['state'],
        js_bookingID: manaList[0]['bookingID'],
        js_meetingRoomID: manaList[0]['meetingRoomID']
    };
    setEle();
}

//正则表达式获取地址栏?后的指定name参数
function GetQueryString(name) {
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

function jumpToBook() {
    /*
    $.ajax({
        method: 'GET',
        async: false,
        //contentType:'',
        url: '/management/toBooking?openid=' + openId,
        success: function (data) {
            console.log(data + 'send successfully');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
     */

    window.location.href = "/management/toBooking?openid=" + openId;
}


function jumpToSearch() {
    /*
    $.ajax({
        method: 'GET',
        async: false,
        //contentType:'',
        url: '/meetingRoom/page?openid=' + openId,
        success: function (data) {
            console.log(data + 'send successfully');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
     */

    window.location.href = "/management/toMeetingRoom?openid=" + openId;
}


function changeToCheck() {
    $("#back").removeClass("block-disable");
    document.getElementById("header").innerText = "审批预约";
    document.getElementById('fatherBox').innerHTML = "";

    pageState = 2;
    getUncheckManaList();
    if (uncheckManaList.length === undefined || uncheckManaList.length === 0) {
        historyPage = historyPage + '<div>无预约请求</div>';
    } else {
        for (let i = 0; i < uncheckManaList.length; i++) {
            mana = {
                js_userID: uncheckManaList[i]['userID'],
                js_userNickname: uncheckManaList[i]['userNickname'],
                js_userRealName: uncheckManaList[i]['userRealName'],
                js_meetingRoomName: uncheckManaList[i]['meetingRoomName'],
                js_bookingPurpose: uncheckManaList[i]['bookingPurpose'],
                js_bookingParticipants: uncheckManaList[i]['bookingParticipants'],
                js_date: uncheckManaList[i]['bookingTimeOccupied']['date'],
                js_beginTime: uncheckManaList[i]['bookingTimeOccupied']['beginTime'],
                js_endTime: uncheckManaList[i]['bookingTimeOccupied']['endTime'],
                js_bookingState: uncheckManaList[i]['bookingState'],
                js_bookingComment: uncheckManaList[i]['bookingComment'],
                js_bookingReply: uncheckManaList[i]['bookingReply'],
                js_state: uncheckManaList[i]['state'],
                js_bookingID: uncheckManaList[i]['bookingID'],
                js_meetingRoomID: uncheckManaList[i]['meetingRoomID']
            };
            checkMoudle = '<div class="text-backcolorYellow detail-margin">\n' +
                '        <div class="container">\n' +
                '            <div class="row">\n' +
                '                <div class="textFont text-title" id="checkDate_' + mana.js_bookingID + '">\n' + mana.js_date +
                '                    \n' +
                '                </div>\n' +
                '                <div class="textFont text-title1" id="checkMeetingRoomName_' + mana.js_bookingID + '">\n' + mana.js_meetingRoomName +
                '                    \n' +
                '                </div>\n' +
                '                <div>\n' +
                '                    <hr class="hr-sty">\n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    参会日期:\n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    参会时间:\n' +
                '                </div>\n' +
                '                <div class="text1">\n' + mana.js_date +
                '                    \n' +
                '                </div>\n' +
                '                <div class="text1" id="checkTime_' + mana.js_bookingID + '">\n' + mana.js_beginTime + '-' + mana.js_endTime +
                '                    \n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    参会人数:\n' +
                '                </div>\n' +
                '                <div class="text1 text-bold">\n' +
                '                    会议目的:\n' +
                '                </div>\n' +
                '                <div class="text1"  id="checkPar_' + mana.js_bookingID + '">\n' + mana.js_bookingParticipants +
                '                    人\n' +
                '                </div>\n' +
                '                <div class="text1" id="checkPur_' + mana.js_bookingID + '">\n' + mana.js_bookingPurpose +
                '                    \n' +
                '                </div>\n' +
                '                <div class="text1 text1-bottom text-bold">\n' +
                '                    申请人说明:\n' +
                '                </div>\n' +
                '                <div class="div-text">\n' +
                '                    <textarea rows="5" cols="20" readonly="readonly" class="textarea1" id="checkCom_' + mana.js_bookingID + '">' + mana.js_bookingComment + '</textarea>\n' +
                '                </div>\n' +
                '                <div class="text1 text1-bottom text-bold">\n' +
                '                    管理员审批意见:\n' +
                '                </div>\n' +
                '                <div class="div-text">\n' +
                '                    <textarea rows="5" cols="20" class="textarea1"  id="checkRep_' + mana.js_bookingID + '">' + mana.js_bookingReply + '</textarea>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '        <div class="btn-block-bottom">\n' +
                '            <div class="btn-block">\n' +
                '                <ul class="ulstyle">\n' +
                '                    <li>\n' +
                '                        <button type="button" class="textRed btn-set1 btn-set-all" name="' + mana.js_bookingID + '" onclick="disagree(this)">不同意</button>\n' +
                '                    </li>\n' +
                '                    <li>\n' +
                '                        <button type="button" class="textYellow btn-set2 btn-set-all" id="opion_' + mana.js_bookingID + '" name="' + mana.js_meetingRoomID + '">意见</button>\n' +
                '                    </li>\n' +
                '                    <li>\n' +
                '                        <button type="button" class="textGreen btn-set3 btn-set-all" name="' + mana.js_bookingID + '" onclick="agree(this)">同意</button>\n' +
                '                    </li>\n' +
                '                </ul>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>'
            //checkPage = checkPage + checkMoudle;
            document.getElementById('fatherBox').innerHTML += checkMoudle;

        }
    }
    $(document).ready(function () {
        $("#fatherBox").addClass("block-updown-padding");
        let t_mana;
        for (let i = 0; i < uncheckManaList.length; i++) {
            t_mana = {
                js_bookingID: uncheckManaList[i]['bookingID'],
                js_bookingState: uncheckManaList[i]['bookingState']
            };
            switch (t_mana.js_bookingState) {
                case 0:
                    $("#checkDate_" + t_mana.js_bookingID).addClass("text-backcolorRed");
                    break;
                case 1:
                    $("#checkDate_" + t_mana.js_bookingID).addClass("text-backcolorYellow");
                    break;
                case 2:
                    $("#checkDate_" + t_mana.js_bookingID).addClass("text-backcolorGreen");
                    break;
            }
        }

    })
    //checkPage = checkPage + Footer;
    //document.getElementById('fatherBox').innerHTML = checkPage;
}

function getUncheckManaList() {
    $.ajax({
        method: 'GET',
        async: false,
        url: '/management/getAllUncheckBookingSubmit?openid=' + openId,
        success: function (data) {
            uncheckManaList = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
}

function getManaList() {
    $.ajax({
        method: 'GET',
        async: false,
        url: '/management/getAllHistoryByOpenID?openid=' + openId,
        success: function (data) {
            manaList = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
}

function refreshUserInfo() {
    let icon = document.getElementById("icon");
    icon.setAttribute("src", user["headimgurl"]);
    document.getElementById("privilege").innerText = user["privilege"] === 0 ? "用户" : "管理员";
    document.getElementById("nickName").innerText = user["nickname"];

}

function open() {
    $("#fatherBox").removeClass("block-padding");
    openId = GetQueryString('openid');
    console.log(openId);
    pageState = 0;
    $.ajax({
        method: 'GET',
        url: '/management/greeting?openid=' + openId,
        async: false,
        success: function (data) {
            manaMainPage = document.getElementById('fatherBox').innerHTML;
            //Footer = document.getElementById('mynav').innerHTML;
            getManaList();
            showRecentMana();//todo
            user = data;
            refreshUserInfo();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //状态码
            console.log(XMLHttpRequest.status);
            // 状态
            console.log(XMLHttpRequest.readyState);
            // 错误信息
            console.log(textStatus);
        }
    });
}

window.onload = open;
